const { parseString } = require('xml2js')

//引入fs模块
const {
    writeFile,
    readFile
} = require('fs');
//引入path模块
const { resolve } = require('path')
module.exports = {
    getUserDataAsync(req) {
        return new Promise((resolve, reject) => {
            let xmlData = ''
            req.on('data', data => {
                    // console.log(data);
                    xmlData += data.toString();
                })
                .on('end', () => {
                    resolve(xmlData)
                })
        })
    },
    parseXMLAsyns(xmlData) {
        return new Promise((resolve, reject) => {
            parseString(xmlData, { trim: true }, (err, data) => {
                if (!err) {
                    resolve(data);
                } else {
                    reject('parseXMLAsyns方法出了问题' + err)
                }
            })
        })
    },
    formatMessage(jsData) {
        let message = {};
        jsData = jsData.xml;
        if (typeof jsData === 'object') {
            for (let key in jsData) {
                let value = jsData[key];
                message[key] = value[0]
            }
        }

        return message;
    },
    writeFileAsync(data, fileName) {
        data = JSON.stringify(data)
        const filePath = resolve(__dirname, fileName)
        return new Promise((resolve, reject) => {
            writeFile(filePath, data, err => {
                if (!err) {
                    // console.log('文件保存成功');
                    resolve();
                } else {
                    reject('writeFileAsync方法出了问题' + err)
                }
            })
        })
    },
    readFileAsync(fileName) {
        const filePath = resolve(__dirname, fileName)
        return new Promise((resolve, reject) => {
            readFile(filePath, (err, data) => {
                if (!err) {
                    // console.log('文件读取成功');
                    data = JSON.parse(data)
                    resolve(data);
                } else {
                    reject('readFileAsync方法出了问题' + err)
                }
            })
        })
    }
}